package netbay;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.servlet.http.HttpSession;

import util.String.StringUtil;
import util.jQgrid.JQGridRow;

public class TransactionLog {

	public static final String PAY_BANK = "PAYBANK";
	public static final String PAY_TCD = "PAYTCD";
	public static final String RD2BANK = "RD2BANK";
	public static final String BANK2RD= "BANK2RD";
	public static final String TCD_Name = "TCD";
	public static final String JPMC_NAME = "JPMC";

	private int Id;
	private String comment;
	private String status;
	private String converId;
	private String tcdAttach;
	private String tcdCtrl;
	private String actionType;

	public Integer getId() {
		return Id;
	}

	public void setId(Integer Id) {
		this.Id = Id;
	}

	public String getConverId() {
		return converId;
	}

	public void setConverId(String converId) {
		this.converId = converId;
	}
	
	public String getActionType() {
		return actionType;
	}

	public void setActionType(String actionType) {
		this.actionType = actionType;
	}
	
	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
	
	public String getTcdAttach() {
		return tcdAttach;
	}
	
	public void setTcdAttach(String tcdAttach) {
		this.tcdAttach = tcdAttach;
	}
	
	public String getTcdCtrl() {
		return tcdCtrl;
	}

	public void setTcdCtrl(String tcdCtrl) {
		this.tcdCtrl = tcdCtrl;
	}

	public String toString() {
		return " [" + getId() + "," + getConverId() + "," + getTcdCtrl()
				+ "," + getTcdAttach()+ "] ";
	}

//	public String genJpmcCtrlName() {
//		String attach = getJpmcAttach();
//		String ctrl = attach.substring(0, attach.length() - 3) + "ctrl";
//		return ctrl;
//	}

	public boolean createJpmcCtrlFile(File ctrlFile,TransactionTraffic traff) {

		if (!ctrlFile.exists()) {
			try {
				ctrlFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		boolean succeed = false;
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter(ctrlFile));

			bw.write("CPA_ID=" + TransactionManager.JPMCcpaId);
			bw.newLine();
			bw.write("SERVICE=" + TransactionManager.JPMCService);
			bw.newLine();
			if(traff.getType().equals("TCD"))bw.write("ACTION=" + WorkerManager.ActionBankToTCD);
			else if(traff.getType().equals("RD"))bw.write("ACTION=" + WorkerManager.ActionBankToRD);
			bw.newLine();
			bw.write("CONV_ID=" + getConverId());
			bw.newLine();
			bw.write("MSG_ID=" + traff.getJpmcMsgId());
			bw.newLine();
			bw.write("REF_MSG_ID=" + traff.getTcdMsgId());
			bw.newLine();
			bw.write("FROM_PARTY_ID=" + traff.getTcdPartyId());
			bw.newLine();
			bw.write("FROM_PARTY_TYPE=" + traff.getTcdPartyType());
			bw.newLine();
			bw.write("TO_PARTY_ID=" + traff.getJpmcPartyId());
			bw.newLine();
			bw.write("TO_PARTY_TYPE=" + traff.getJpmcPartyType());
			bw.newLine();
			bw.write("TRANSPORT_PROTOCOL="
					+ TransactionManager.JPMCTransportProtocol);
			bw.newLine();
			bw.write("TRANSPORT_ENDPOINT="
					+ TransactionManager.JPMCTransportEndPoint);
			bw.newLine();
			bw.write("PAYLOAD_TYPE=" + TransactionManager.JPMCPayloadType);
			bw.newLine();
			bw.write("PAYLOAD_NAME=" + traff.getJpmcAttach());

			
			succeed = true;

		} catch (FileNotFoundException e) {
			e.printStackTrace();
			succeed = false;
		} catch (IOException e) {
			e.printStackTrace();
			succeed = false;
		}
		finally{
			if(bw!=null)
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		
		return succeed;
	}
		
	public boolean setTimeEntry(String timeProperty,TransactionTraffic traff,String type)
	{
		Integer transactionId = getId();
		
		String action = "";
		String process = "";
		
		if(timeProperty.equalsIgnoreCase(TransactionManager.recvTcdStatus))
		{
			action = PAY_TCD;
			process = TransactionManager.recvTcdStatus;
		}
		else if(timeProperty.equalsIgnoreCase(TransactionManager.beginSendJpmcStatus) && traff.getType().equals("TCD"))
		{
			action = PAY_TCD;
			process = TransactionManager.beginSendJpmcStatus;
		}
		else if(timeProperty.equalsIgnoreCase(TransactionManager.finSendJpmcStatus) && traff.getType().equals("TCD"))
		{
			action = PAY_TCD;
			process = TransactionManager.finSendJpmcStatus;
		}
		else if(timeProperty.equalsIgnoreCase(TransactionManager.recvJpmcStatus) && traff.getType().equals("TCD"))
		{
			action = PAY_BANK;
			process = TransactionManager.recvJpmcStatus;
		}
		else if(timeProperty.equalsIgnoreCase(TransactionManager.sendTcdStatus))
		{
			action = PAY_BANK;
			process = TransactionManager.sendTcdStatus;
		}
		else if(timeProperty.equalsIgnoreCase(TransactionManager.resendSFTPStatus) && traff.getType().equals("TCD"))
		{
			action = PAY_TCD;
			process = TransactionManager.resendSFTPStatus;
		}
		else if(timeProperty.equalsIgnoreCase(TransactionManager.resendFinSFTPStatus) && traff.getType().equals("TCD"))
		{
			action = PAY_TCD;
			process = TransactionManager.resendFinSFTPStatus;
		}
		else if(timeProperty.equalsIgnoreCase(TransactionManager.redownloadSFTPStatus))
		{
			action = PAY_BANK;
			process = TransactionManager.redownloadSFTPStatus;
		}
		else if(timeProperty.equalsIgnoreCase(TransactionManager.redownloadFinSFTPStatus))
		{
			action = PAY_BANK;
			process = TransactionManager.redownloadFinSFTPStatus;
		}
		
		else if(timeProperty.equalsIgnoreCase(TransactionManager.recvRdStatus))
		{
			action = RD2BANK;
			process = TransactionManager.recvRdStatus;
		}
		else if(timeProperty.equalsIgnoreCase(TransactionManager.beginSendJpmcStatusRD) && traff.getType().equals("RD"))
		{
			action = RD2BANK;
			process = TransactionManager.beginSendJpmcStatusRD;
		}
		else if(timeProperty.equalsIgnoreCase(TransactionManager.finSendJpmcStatusRD) && traff.getType().equals("RD"))
		{
			action = RD2BANK;
			process = TransactionManager.finSendJpmcStatusRD;
		}
		else if(timeProperty.equalsIgnoreCase(TransactionManager.recvJpmcStatusRD) && traff.getType().equals("RD"))
		{
			action = BANK2RD;
			process = TransactionManager.recvJpmcStatusRD;
		}
		else if(timeProperty.equalsIgnoreCase(TransactionManager.sendRdStatus))
		{
			action = BANK2RD;
			process = TransactionManager.sendRdStatus;
		}
		else if(timeProperty.equalsIgnoreCase(TransactionManager.finSendRdStatus))
		{
			action = BANK2RD;
			process = TransactionManager.finSendRdStatus;
		}
		else if(timeProperty.equalsIgnoreCase(TransactionManager.resendSFTPStatusRD) && traff.getType().equals("RD"))
		{
			action = RD2BANK;
			process = TransactionManager.resendSFTPStatusRD;
		}
		else if(timeProperty.equalsIgnoreCase(TransactionManager.resendFinSFTPStatusRD) && traff.getType().equals("RD"))
		{
			action = RD2BANK;
			process = TransactionManager.resendFinSFTPStatusRD;
		}
		else if(timeProperty.equalsIgnoreCase(TransactionManager.redownloadSFTPStatusRD))
		{
			action = BANK2RD;
			process = TransactionManager.redownloadSFTPStatusRD;
		}
		else if(timeProperty.equalsIgnoreCase(TransactionManager.redownloadFinSFTPStatusRD))
		{
			action = BANK2RD;
			process = TransactionManager.redownloadFinSFTPStatusRD;
		}
		
		traff.setAction(action);
		traff.setProcess(process);
		traff.setEventTime(TransactionManager.getCurTimeDecimal());
		TransactionManager manager = new TransactionManager();
		if(type.equals(""))manager.updateStatus(this, process);
		return manager.insertTraffic(traff);
	}

	public static PreparedStatement convertFindById(Integer id, Connection con)
			throws SQLException {

		PreparedStatement st = con
				.prepareStatement("select * from transaction where id = ?");
		st.setInt(1, id);
		
		return st;

	}

	public static PreparedStatement convertFindByConverId(String converId,
			Connection con) throws SQLException {
		PreparedStatement st = con
				.prepareStatement("select * from transaction where conversationid = ?");
		st.setString(1, converId);
		return st;

	}
	
	public static PreparedStatement convertFindDoneByConverId(String converId,
			Connection con) throws SQLException {
		PreparedStatement st = con
				.prepareStatement("select * from transaction where conversationid = ? and (status = ? or status = ?) ");
		st.setString(1, converId);
		st.setString(2, TransactionManager.finSendJpmcStatus);
		st.setString(3, TransactionManager.finSendJpmcStatusRD);
		return st;

	}
	
	public static PreparedStatement sqlFindNotUpdateTransaction(Connection con) throws SQLException {
		PreparedStatement st = con
				.prepareStatement("select conversationid from transaction where status = ?");
		
		st.setString(1, TransactionManager.sendTcdStatus);
		
		return st;

	}
	
	
	public static List convertResultsetNotUpdateToList(ResultSet rs)
	throws SQLException {
		List displayList = new ArrayList();
		while (rs.next()) {
			String conversationid  = rs.getString("conversationid");	
			displayList.add(conversationid);
		}
	
	return displayList;
}
	
	public static PreparedStatement convertFindByConverId(String converId, String page,
			String limit, String sidx, String sord,
			Connection con) throws SQLException {
		Integer startList = Integer.parseInt(limit) * (Integer.parseInt(page) - 1);
        Integer endList = Integer.parseInt(limit) * Integer.parseInt(page);
        
        
		String strOrderBy = " ORDER BY transaction." + sidx + " " + sord;
		String strLimit = " LIMIT "+startList.toString()+", "+endList.toString();
		
		String strSql = "select * from transaction  where conversationid = ?  ";
        strSql += strOrderBy;
        strSql += strLimit;
		
		PreparedStatement st = con
				.prepareStatement(strSql);
		st.setString(1, converId);

		return st;

	}
	public static PreparedStatement convertFindByCondition(String fromTime,String toTime,String type,String status,String converID,
			String organize,String page,String limit, String sidx, String sord,
			Connection con) throws SQLException {
		Integer startList = Integer.parseInt(limit) * (Integer.parseInt(page) - 1);
        Integer endList = Integer.parseInt(limit) * Integer.parseInt(page);
        
        String strOrderBy = "";
        if(sidx.equals("time"))strOrderBy = " ORDER BY tf." + sidx + " " + sord;
        else strOrderBy = " ORDER BY ts." + sidx + " " + sord;
		String strLimit = " LIMIT "+startList.toString()+", "+endList.toString();
		
		String strSql = "select ts.conversationid,ts.status,tf.* from transaction as ts left join traffic as tf on ts.id = tf.transactionid where ";
		String fromTimeCondition =((fromTime.equals("000000.000")||fromTime==null)?"true and ":" tf.time >="+fromTime+" and ");
		String toTimeCondition = ((toTime.equals("999999.999")||toTime==null)?"true and ":" tf.time <="+toTime+" and ");
		String typeCondition = ((type.equals(""))?"true and ":" tf.action='"+type+"' and ");
		String statusCondition = ((status.equals(""))?"true and ":"ts.status='"+status+"' and ");
		String converIDCondition = ((converID.equals(""))?"true and ":"ts.conversationid='"+converID+"' and ");
		String exceptCondition = "tf.process != ? and tf.process != ? and tf.process != ? and tf.process !=  ? and ";
		String lastCondition = " tf.time = (select max(time) from traffic where ";
		String lastConditionFromTime = ((fromTime.equals("000000.000")||fromTime==null)?"true and ":" time >="+fromTime+" and ");
		String lastConditionToTime = ((toTime.equals("999999.999")||toTime==null)?"true and ":" time <="+toTime+" and ");
		String lastConditionType = ((type.equals(""))?"true and ":" action='"+type+"' and ");
		String lastConditionStatus = ((status.equals(""))?"true and ":"ts.status='"+status+"' and ");
		String lastConditionExcept = "process != ? and process != ? and process != ? and process !=  ? and ";
		String lastCondition2 = " transactionid=ts.id group by transactionid) and ";
		String organizeCondition = "ts.type='"+organize+"'";
		
		strSql += fromTimeCondition;
		strSql += toTimeCondition;
		strSql += typeCondition;
		strSql += statusCondition;
		strSql += converIDCondition;
		strSql += exceptCondition;
		strSql += lastCondition;
		strSql += lastConditionFromTime;
		strSql += lastConditionToTime;
		strSql += lastConditionType;
		strSql += lastConditionStatus;
		strSql += lastConditionExcept;
		strSql += lastCondition2;
		strSql += organizeCondition;
		strSql += strOrderBy;
        strSql += strLimit;
		
		PreparedStatement st = con
				.prepareStatement(strSql);
		st.setString(1, TransactionManager.resendSFTPStatus);
		st.setString(2, TransactionManager.resendFinSFTPStatus);
		st.setString(3, TransactionManager.redownloadSFTPStatus);
		st.setString(4, TransactionManager.redownloadFinSFTPStatus);
		st.setString(5, TransactionManager.resendSFTPStatus);
		st.setString(6, TransactionManager.resendFinSFTPStatus);
		st.setString(7, TransactionManager.redownloadSFTPStatus);
		st.setString(8, TransactionManager.redownloadFinSFTPStatus);

		System.out.println(st);
		return st;

	}
	
	public static PreparedStatement findCtrlToResend(String converId,
			Connection con) throws SQLException {
		PreparedStatement st = con
				.prepareStatement("select id,tcdctrl from transaction where conversationid = ?");
		st.setString(1, converId);

		return st;

	}
	
	public static PreparedStatement convertFindByCondition(String fromTime,String toTime,String type,String status,String converID,
String organize,Connection con) throws SQLException {

		String strSql = "select count(*) as sum from transaction as ts left join traffic as tf on ts.id = tf.transactionid where ";
		String fromTimeCondition =((fromTime.equals("000000.000")||fromTime==null)?"true and ":" tf.time >="+fromTime+" and ");
		String toTimeCondition = ((toTime.equals("999999.999")||toTime==null)?"true and ":" tf.time <="+toTime+" and ");
		String typeCondition = ((type.equals(""))?"true and ":" tf.action='"+type+"' and ");
		String statusCondition = ((status.equals(""))?"true and ":"ts.status='"+status+"' and ");
		String converIDCondition = ((converID.equals(""))?"true and ":"ts.conversationid='"+converID+"' and ");
		String exceptCondition = "tf.process != ? and tf.process != ? and tf.process != ? and tf.process != ? and ";
		String lastCondition = " tf.time = (select max(time) from traffic where ";
		String lastConditionFromTime = ((fromTime.equals("000000.000")||fromTime==null)?"true and ":" time >="+fromTime+" and ");
		String lastConditionToTime = ((toTime.equals("999999.999")||toTime==null)?"true and ":" time <="+toTime+" and ");
		String lastCoditionType = ((type.equals(""))?"true and ":" action='"+type+"' and ");
		String lastCoditionStatus = ((status.equals(""))?"true and ":"ts.status='"+status+"' and ");
		String lastConditionExcept = "process != ? and process != ? and process != ? and process != ? and ";
		String lastCondition2 = " transactionid=ts.id group by transactionid) and ";
		String organizeCondition = "ts.type='"+organize+"'";
		strSql += fromTimeCondition;
		strSql += toTimeCondition;
		strSql += typeCondition;
		strSql += statusCondition;
		strSql += converIDCondition;
		strSql += exceptCondition;
		strSql += lastCondition;
		strSql += lastConditionFromTime;
		strSql += lastConditionToTime;
		strSql += lastCoditionType;
		strSql += lastCoditionStatus;
		strSql += lastConditionExcept;
		strSql += statusCondition;
		strSql += lastCondition2;
		strSql += organizeCondition;

		PreparedStatement st = con
				.prepareStatement(strSql);
		st.setString(1, TransactionManager.resendSFTPStatus);
		st.setString(2, TransactionManager.resendFinSFTPStatus);
		st.setString(3, TransactionManager.redownloadSFTPStatus);
		st.setString(4, TransactionManager.redownloadFinSFTPStatus);
		st.setString(5, TransactionManager.resendSFTPStatus);
		st.setString(6, TransactionManager.resendFinSFTPStatus);
		st.setString(7, TransactionManager.redownloadSFTPStatus);
		st.setString(8, TransactionManager.redownloadFinSFTPStatus);
		return st;

	}
	
	
	
	public static PreparedStatement convertFindAdmin(
			String page,
			String limit, String sidx, String sord,Connection con) throws SQLException {

		Integer startList = Integer.parseInt(limit) * (Integer.parseInt(page) - 1);
        Integer endList = Integer.parseInt(limit) * Integer.parseInt(page);
        
        
		String strOrderBy = " ORDER BY admin." + sidx + " " + sord;
		String strLimit = " LIMIT "+startList.toString()+", "+endList.toString();
		
		String strSql = "select * from admin where id != 1  ";
        strSql += strOrderBy;
        strSql += strLimit;
		
        PreparedStatement st = con.prepareStatement(strSql);
        

		return st;

	}
	public static PreparedStatement convertToTimestamp(
			String dbl,Connection con) throws SQLException {
		
		String strSql = "select timestamp(?)as timestamp from traffic  ";

		
        PreparedStatement st = con.prepareStatement(strSql);
        
        st.setString(1, dbl);
		return st;

	}
	
	public static PreparedStatement sqlGetTransactionIdByConverId(
			String converId,Connection con) throws SQLException {
		
		String strSql = "select id from transaction where conversationid = ? ";

		
        PreparedStatement st = con.prepareStatement(strSql);
        
        st.setString(1, converId);
		return st;

	}

	public static String convertInsert(TransactionLog log) throws SQLException {
		String type = "";
		if(log.getActionType() != null && log.getActionType().equals(WorkerManager.ActionTCDToBank)){
			type = "TCD";
		}else if(log.getActionType() != null && log.getActionType().equals(WorkerManager.ActionRDToBank)){
			type = "RD";
		}else{
			return null;
		}
		String query = "insert into transaction( id, conversationid ,type)"
		+ "values( NULL" + ", \""
		+ log.getConverId() + "\" , \"" 
		+ type + "\""
		+		")";
		return query;
	}
	
	public static List convertResultsetToDisplay(ResultSet rs,String reJPMCTCD,String reTCDTCD)
		throws SQLException {
		
		//List<String[]> displayList = new ArrayList<String[]>();
		List displayList = new ArrayList();
		JQGridRow row;
		DateFormat format = new SimpleDateFormat("yy-mm-dd hh:mm:ss");
		while (rs.next()) {
			
			row = new JQGridRow();
			List display = new ArrayList();
			display.add((String) rs.getObject("conversationid"));
			display.add((String) rs.getObject("status"));
			String timeStr = rs.getString("time");
			display.add(TransactionManager.convertStrToTmsStr(timeStr));
			
			if(reJPMCTCD.equals("0") && reTCDTCD.equals("0")){
				
			}
			if(((String) rs.getObject("tcdattach"))!= null){
				display.add((String) rs.getObject("convertfile"));
				if(reJPMCTCD.equals("0")){
					display.add("0");
				}else display.add("1");

				display.add("0");
			}
			else{
				display.add((String) rs.getObject("convertfile"));
				if(reJPMCTCD.equals("0"))display.add("0");
				else display.add("1");
				if(reTCDTCD.equals("0"))display.add("0");
				else display.add("1");
			}
			row.setCell(display);
			displayList.add(row);
		//	String[] display = new String[5];
		//	display[0] = ((String) rs.getObject("conversationid"));
		//	display[1] = ((String) rs.getObject("status"));
		//	display[2] = ((String) rs.getObject("tcdmsgid"));
		//	display[3] = ((String) rs.getObject("jpmcmsgid"));
		//	display[4] = new Integer(rs.getInt("id")).toString();
		//	
		//	displayList.add(display);
		}
		return displayList;
	}
// Obsolete because change the requirement
	
//	public PreparedStatement convertUpdate(String timePropertyName, Connection con) throws SQLException {
//
//		System.out.println(" setting : " + timePropertyName);
//
//		PreparedStatement st = con.prepareStatement("update transaction set "
//				+ timePropertyName + " = ? , status = ? where id = " + getId());
//		
//		st.setTimestamp(1, new Timestamp(new Date().getTime()));
//				
//		String status="";
//		
//		if(timePropertyName.equalsIgnoreCase(TransactionLog.RECV_TCD_TIME))
//		{
//			status = TransactionManager.recvTcdStatus;
//		}
//		else if(timePropertyName.equalsIgnoreCase(TransactionLog.BEGIN_SEND_JPMC_TIME))
//		{
//			status = TransactionManager.beginSendJpmcStatus;
//		}
//		else if(timePropertyName.equalsIgnoreCase(TransactionLog.FIN_SEND_JPMC_TIME))
//		{
//			status = TransactionManager.finSendJpmcStatus;
//		}
//		else if(timePropertyName.equalsIgnoreCase(TransactionLog.RECV_JPMC_TIME))
//		{
//			status = TransactionManager.recvJpmcStatus;
//		}
//		else if(timePropertyName.equalsIgnoreCase(TransactionLog.SEND_TCD_TIME))
//		{
//			status = TransactionManager.sendTcdStatus;
//		}
//		
//		st.setString(2, status);
//
//		return st;
//	}

	public static TransactionLog convertTcdCtrlFile(File ctrlFile) {
		Properties prop = new Properties();
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(ctrlFile);
			if (fis != null) {
				prop.load(fis);
			}
		} catch (FileNotFoundException e) {
			System.out.println("error file not found in convert");
			e.printStackTrace();
			return null;
		} catch (IOException e2) {
			System.out.println("error IO not found in convert");
			e2.printStackTrace();
			return null;
		} finally {
			try {
				if (fis != null) {
					fis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		TransactionLog newLog = new TransactionLog();
		if (prop.getProperty("CONV_ID") != null)
			newLog.setConverId(prop.getProperty("CONV_ID"));
		if (prop.getProperty("PAYLOAD_NAME") != null)
			newLog.setTcdAttach(prop.getProperty("PAYLOAD_NAME"));
		if (prop.getProperty("ACTION") != null)
			newLog.setActionType(prop.getProperty("ACTION"));

		newLog.setTcdCtrl(ctrlFile.getName());

		return newLog;
	}
	
	public static String getConverId(File ctrlFile) {
		Properties prop = new Properties();
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(ctrlFile);
			if (fis != null) {
				prop.load(fis);
			}
		} catch (FileNotFoundException e) {
			System.out.println("error file not found in convert");
			e.printStackTrace();
			return null;
		} catch (IOException e2) {
			System.out.println("error IO not found in convert");
			e2.printStackTrace();
			return null;
		} finally {
			try {
				if (fis != null) {
					fis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String converId = prop.getProperty("CONV_ID");
		
		return converId;
	}

	public static TransactionLog convertResultsetToLog(ResultSet rs) {
		TransactionLog log = null;

		try {
			if (rs.next()) {
				log = new TransactionLog();
				log.setId(rs.getInt("id"));
				log.setStatus(rs.getString("status"));
				log.setConverId(rs.getString("conversationid"));
				if(rs.getString("type").equals("TCD")){
					log.setActionType(WorkerManager.ActionBankToTCD);
				}else if(rs.getString("type").equals("RD")){
					log.setActionType(WorkerManager.ActionBankToRD);
				}else{
					return null;
				}

				return log;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;

	}
	
	public static TransactionLog convertResultsetToLogFindTransactionById(ResultSet rs) {
		TransactionLog log = new TransactionLog();

		try {
			if (rs.next()) {
				log.setId(rs.getInt("id"));
				log.setStatus(rs.getString("status"));
				log.setConverId(rs.getString("conversationid"));
				if(rs.getString("type").equals("TCD")){
					log.setActionType(WorkerManager.ActionTCDToBank);
				}else if(rs.getString("type").equals("RD")){
					log.setActionType(WorkerManager.ActionRDToBank);
				}else{
					return log;
				}
				return log;
			}else{
				return null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return log;

	}

	public PreparedStatement convertUpdateStatus(Connection con) {
		PreparedStatement st = null;
		try {
			st = con.prepareStatement("update transaction set status = ? where id = ?");
		
		st.setString(1, getStatus());
		st.setInt(2, getId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return st;
	}
	
	public static PreparedStatement sqlUpdateTransactionStatus(String conversationid ,String type,Connection con) {
		PreparedStatement st = null;
		try {
			st = con.prepareStatement("update transaction set status = ? where conversationid = ?");
			if(type.equals("TCD")){
				st.setString(1, TransactionManager.finSendTcdStatus);
			}else if(type.equals("RD")){
				st.setString(1, TransactionManager.finSendRdStatus);
			}
			st.setString(2,conversationid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return st;
	}
	
	
	
	
}
